<?php

	require_once dirname(__FILE__) . '/Connection.php';
	require_once dirname(__FILE__) . '/../model/Modalidade.php';
	
	class ModalidadePDO
	{
		private $connection = NULL;
		
		public function __construct()
		{
			$this->connection = Connection::getInstance();
		}
		
		public function inserir($nome)
		{
			try {
				$this->connection->beginTransaction();
				$stmt = $this->connection->prepare("INSERT INTO modalidade(nome) VALUES (:nome)");
				$stmt->bindValue('nome', $nome);
				$stmt->execute();
				$this->connection->commit();
			}
			catch(Exception $erro){
				$this->connection->rollback();
			}
		}
		
		public function alterar($modalidade)
		{
			try {
				$this->connection->beginTransaction();
				$stmt = $this->connection->prepare("UPDATE modalidade SET nome=:nome WHERE id=:id");
				$stmt->bindValue('nome', $modalidade->nome);
				$stmt->bindValue('id', $modalidade->id);
				$stmt->execute();
				$this->connection->commit();
			}
			catch(Exception $erro){
				$this->connection->rollback();
			}	
		}
		
		public function deletar($id)
		{
			try {
				$this->connection->beginTransaction();
				$stmt = $this->connection->prepare("DELETE FROM modalidade WHERE id=:id");
				$stmt->bindValue('id', $id);
				$stmt->execute();
				$this->connection->commit();
			}
			catch(Exception $erro){
				$this->connection->rollback();
			}	
		}
		
		public function pesquisar($busca)
		{
			$consulta = $this->connection->prepare("SELECT * FROM modalidade WHERE nome LIKE :busca ORDER BY nome");
			$consulta->bindValue('busca', ('%'.$busca.'%'));
			$consulta->execute();
			return  $consulta->fetchAll(PDO::FETCH_CLASS,'Modalidade');
		}
		
		public function listar()
		{
			$consulta = $this->connection->prepare("SELECT * FROM modalidade ORDER BY nome");
			$consulta->execute();
			return $consulta->fetchAll(PDO::FETCH_CLASS,'Modalidade');
		}
	}
?>